package com.sunny.ihealth.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.sunny.ihealth.R;
import com.sunny.ihealth.entity.bmob.Comment;
import com.sunny.ihealth.entity.bmob.Post;
import com.sunny.ihealth.entity.bmob.State;
import com.sunny.ihealth.ui.activity.PhotoViewActivity;
import com.sunny.ihealth.utils.TimeUtils;
import com.sunny.ninepictureview.NinePictureView;

import java.util.List;
import java.util.Locale;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 帖子详细信息的adapter
 * Created by Sunny  An on 2016/6/3.
 */
public class PostDetailAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {

    /**
     * header所在的位置
     */
    public static final int POSITION_HEADER = -1;
    private static final int TYPE_HEADER = 0;
    private static final int TYPE_NORMAL = 1;

    private Context context;
    //显示在header的post信息
    private Post post;
    private List<Comment> data;
    private LayoutInflater inflater;

    private OnViewClickListener listener;

    public PostDetailAdapter(Context context, Post post, List<Comment> data) {
        this.context = context;
        this.post = post;
        this.data = data;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_HEADER) {
            return new HeaderHolder(inflater.inflate(R.layout.item_post_detail, parent, false));
        } else {
            return new ItemHolder(inflater.inflate(R.layout.item_comment, parent, false));
        }
    }

    @Override
    public int getItemViewType(int position) {
        return position == 0 ? TYPE_HEADER : TYPE_NORMAL;
    }

    @Override
    public int getItemCount() {
        return data.size() + 1;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position) == TYPE_HEADER) { //header

            HeaderHolder headerHolder = (HeaderHolder) holder;

            if (post.getAuthor().hasHead()) {
                Picasso.with(context).load(post.getAuthor().getHeadFile().getUrl()).into(headerHolder.ivHead);
            }
            headerHolder.tvAuthor.setText(post.getAuthor().getNickname());
            headerHolder.tvTime.setText(TimeUtils.getFriendlyTime(post.getCreatedAt()));
            headerHolder.tvContent.setText(post.getContent());
            if (TextUtils.isEmpty(post.getContent())) {
                headerHolder.tvContent.setVisibility(View.GONE);
            } else {
                headerHolder.tvContent.setVisibility(View.VISIBLE);
            }
            //NinePictureView
            if (post.getPictures() != null && post.getPictures().size() != 0) {
                headerHolder.ninePictureView.setVisibility(View.VISIBLE);
                PostNinePicAdapter adapter = new PostNinePicAdapter(context, post.getPictures());
                headerHolder.ninePictureView.setAdapter(adapter);
            } else {
                headerHolder.ninePictureView.setVisibility(View.GONE);
            }

            String likeStr;
            if (post.isLiked()) {
                likeStr = String.format(Locale.getDefault(), "已赞(%d)", post.getLikeNumber());
            } else {
                likeStr = String.format(Locale.getDefault(), "赞(%d)", post.getLikeNumber());
            }
            headerHolder.btnLike.setText(likeStr);

            if (post.getCommentNumber() == 0) {
                headerHolder.tvDivider.setText("还没有评论");
            } else {
                String dividerTitle = String.format(Locale.getDefault(), "最新评论(%s)", post.getCommentNumber());
                headerHolder.tvDivider.setText(dividerTitle);
            }

            int state = post.getAuthor().isFriend();
            if (state == State.STATE_BOTH_FRIEND) {
                headerHolder.tvAddFriend.setText("互相关注");
                headerHolder.tvAddFriend.setTextColor(context.getResources().getColor(R.color.lightGray));
            } else if (state == State.STATE_ADD) {
                headerHolder.tvAddFriend.setText("已关注");
                headerHolder.tvAddFriend.setTextColor(context.getResources().getColor(R.color.lightGray));
            } else {
                headerHolder.tvAddFriend.setText("+关注");
                headerHolder.tvAddFriend.setTextColor(context.getResources().getColor(R.color.addFriendBlue));
            }

            headerHolder.tvAddFriend.setOnClickListener(this);
            headerHolder.btnLike.setOnClickListener(this);
            headerHolder.ninePictureView.setOnItemClickListener(new NinePictureView.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    Intent intent = new Intent(context, PhotoViewActivity.class);
                    intent.putExtra("imgs", post.getListPictures());
                    intent.putExtra("position", position);
                    context.startActivity(intent);
                }
            });
            headerHolder.setTag(position);

        } else { //item
            ItemHolder itemHolder = (ItemHolder) holder;
            position = position - 1;
            Comment comment = data.get(position);

            if (comment.getAuthor().hasHead()) {
                Picasso.with(context).load(comment.getAuthor().getHeadFile().getUrl()).into(itemHolder.ivHead);
            }
            itemHolder.tvAuthor.setText(comment.getAuthor().getNickname());
            if (post.getAuthor().getObjectId().equals(comment.getAuthor().getObjectId())) {
                itemHolder.tvIsAuthor.setVisibility(View.VISIBLE);
            } else {
                itemHolder.tvIsAuthor.setVisibility(View.GONE);
            }
            itemHolder.tvTime.setText(TimeUtils.getFriendlyTime(comment.getCreatedAt()));
            itemHolder.tvContent.setText(comment.getContent());

            itemHolder.setTag(position);
        }
    }

    public OnViewClickListener getListener() {
        return listener;
    }

    /**
     * 如点击的Header，position == POSITION_HEADER
     */
    public void setListener(OnViewClickListener listener) {
        this.listener = listener;
    }

    @Override
    public void onClick(View v) {
        if (listener == null) {
            return;
        }
        int position = (int) v.getTag();
        listener.onViewClick(v.getId(), getItemViewType(position) == TYPE_HEADER ? POSITION_HEADER : position - 1);
    }


    /**
     * 设置喜欢按钮的喜欢数量
     *
     * @param increase 加一为true  减一为false
     */
    public void setLikeBtnText(boolean increase) {
        int likeNumber = post.getLikeNumber();
        if (increase) {
            post.setLikeNumber(likeNumber + 1);
            post.setLiked(true);
        } else {
            post.setLikeNumber(likeNumber <= 0 ? 0 : likeNumber - 1);
            post.setLiked(false);
        }
        notifyItemChanged(0, R.id.btn_postDetail_like);
    }

    /**
     * 设置添加好友
     *
     * @param isFriend 是否是好友
     */
    public void setIsFriend(int isFriend) {
        post.getAuthor().setIsFriend(isFriend);
        notifyItemChanged(0, R.id.tv_post_addFriend);
    }


    static class ItemHolder extends RecyclerView.ViewHolder {
        CircleImageView ivHead;
        TextView tvAuthor, tvIsAuthor, tvTime, tvContent;

        public ItemHolder(View v) {
            super(v);
            ivHead = (CircleImageView) v.findViewById(R.id.image_comment_head);
            tvAuthor = (TextView) v.findViewById(R.id.tv_comment_author);
            tvIsAuthor = (TextView) v.findViewById(R.id.tv_comment_isAuthor);
            tvTime = (TextView) v.findViewById(R.id.tv_comment_time);
            tvContent = (TextView) v.findViewById(R.id.tv_comment_content);
        }

        public void setTag(Object tag) {
            ivHead.setTag(tag);
            tvAuthor.setTag(tag);
        }

    }


    static class HeaderHolder extends RecyclerView.ViewHolder {
        CircleImageView ivHead;
        TextView tvAuthor, tvTime, tvContent;
        NinePictureView ninePictureView;
        Button btnLike;
        TextView tvDivider;
        TextView tvAddFriend;

        public HeaderHolder(View v) {
            super(v);
            ivHead = (CircleImageView) v.findViewById(R.id.image_postDetail_head);
            tvAuthor = (TextView) v.findViewById(R.id.tv_postDetail_author);
            tvTime = (TextView) v.findViewById(R.id.tv_postDetail_time);
            tvContent = (TextView) v.findViewById(R.id.tv_postDetail_content);
            ninePictureView = (NinePictureView) v.findViewById(R.id.ninePicView_postDetail);
            btnLike = (Button) v.findViewById(R.id.btn_postDetail_like);
            tvDivider = (TextView) v.findViewById(R.id.tv_postDetail_commentNumber);
            tvAddFriend = (TextView) v.findViewById(R.id.tv_postDetail_addFriend);
        }

        public void setTag(Object tag) {
            ivHead.setTag(tag);
            tvAuthor.setTag(tag);
            btnLike.setTag(tag);
            tvAddFriend.setTag(tag);
        }
    }

}
